<?php
namespace Home\Controller;

use Think\Controller;

class IndexController extends Controller
{

      public function _initialize(){
        if (!session('?islogin')) {
       $this->error('请先登陆',U('Home/User/denglu'));
        }
      }



    public function index()
    {

        $form = M('s');
       $where=array_filter(I('get.'));
       if (isset($where['name'])) {
            $where['name']=array('like',"%{$where['name']}%");
       }
       $minage=isset($where['minage'])?$where['minage']:0;
       $maxage=isset($where['maxage'])?$where['maxage']:100;
       $where['age']=array(array('gt',$minage),array('lt',$maxage));
     

       $count=M('s')->where($where)->count();
       $Page       = new \Think\Page($count,4);
       $arr= M('s')->where($where)->order('id')->limit($Page->firstRow.','.$Page->listRows)->select();
          $show       = $Page->show();
        $this->assign('arr',$arr);

         if (IS_AJAX) {
            if ($arr&&I('get.p')>0) {
                $return['data']=$this->fetch('ajaxPage');
                $return['info']='查询成功';
                $return['status']=1;
                $return['xxx']=$arr;
                $return['p']=I('get.p');
            }
            else{
               $return['data']=null;
                $return['info']='查询失败';
                $return['status']=0;
            }
            $this->ajaxReturn($return);
         }


         $this->assign('show',$show);
        $this->display();
      
    }
  
    public function del($id)
    {
        $form = M('s');
        $res  = $form->delete($id);
        if ($res) {
           $this->success( '删除成功',U('Home/Index/index'));

        } else {
             $this->error( '删除失败');

        }
    } 
     public function add(){
    	$this->display('form');
    }
    public function doadd()
    {
        $form          = M('s');
        $res           = $form->add(I('post.'));
        if ($res) {
            $this->success( '增加成功',U('Home/Index/index'));
        } else {
           $this->error( '增加失败');

        }
    }
    public function detail(){
    	$form = M('s');
        $arr  = $form->find();
        $this->assign('arr', $arr);
    	$this->display();
    }
    public function update($id){
         $form = M('s');
        $res  = $form->find($id);
        
         $this->assign('res', $res);
         $this->display();
    }
   public function doupdate(){
           $form = M('s');
    
           $res=$form->where(['id'=>I('post.id')])->save(I('post.'));
              if ($res) {
            $this->success( '增加成功',U('Home/Index/index'));
        } else {
           $this->error( '增加失败');

        }
    }
   

  
}
